package com.bringway.p2p.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.sf.json.JSONObject;

import com.bringway.fm.dao.AttDao;
import com.bringway.framework.domain.SysBizAtt;
import com.bringway.global.dao.SysAttechmentDao;
import com.bringway.p2p.dao.ZqshcgDao;
import com.bringway.p2p.dao.ZqzrlbDao;
import com.bringway.p2p.dao.impl.ZqshcgDaoImpl;
import com.bringway.p2p.service.ZqshcgService;



/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月16日 上午9:34:42
 * @功能：债权收益界面
 */

@Service
@Transactional
public class ZqshcgServieImpl implements ZqshcgService{
	
	Logger logger = Logger.getLogger(ZqshcgDaoImpl.class);
	
	@Autowired
	private SysAttechmentDao sysAttechmentDao;
	
	@Autowired
	private ZqshcgDao zqshcgDao;
	
	@Autowired
	private ZqzrlbDao zqzrlbDao;

	@Override
	public List queryZqshjgList(JSONObject params) {
		return zqshcgDao.queryZqshjgList(params);
	}

	@Override
	public int getZqshjCount(JSONObject params) {
		return zqshcgDao.getZqshjCount(params);
	}

	@Override
	public String saveZqshj(JSONObject params) {
		String bizId =  zqshcgDao.saveZqshj(params);
		String atts = params.optString("atts");
		String[] att = atts.split(",");
		for (int i = 0; i < att.length; i++) {
			if("".equals(att[i]))continue;
			SysBizAtt bizAtt = new SysBizAtt();
			bizAtt.setBizId(bizId);
			bizAtt.setAttId(Long.decode(att[i]));
			sysAttechmentDao.createBizAtt(bizAtt);
		}
		return bizId;
	}

	@Override
	public void deleteZqshcg(JSONObject params) {
		zqshcgDao.deleteZqshcg(params);
	}

	@Override
	public Map<String, Object> getZqshcg(JSONObject params) {
		return zqshcgDao.getZqshcg(params);
	}

	private static byte[] lock = new byte[0]; 
	@Transactional("p2p")
	public boolean saveSHZqshcg(JSONObject params) {
		boolean isSaveSHZqshcgSuc = false;
		boolean isInsertZqSuc = false;
		synchronized (lock) {
		Map<String, Object> zqshcgMap = zqshcgDao.getZqshcg(params);
		if (null == zqshcgMap) {
			logger.info("信息有误,审核债权失败！ ");
			return false;
		} else {
			String state =MapUtils.getString(zqshcgMap, "STATE");
			if ("01".equals(state)) {
				isSaveSHZqshcgSuc = zqshcgDao.saveSHZqshcg(params);//更改审核状态
				String shState = params.optString("shState");
				if(isSaveSHZqshcgSuc && "02".equals(shState)){//草稿审核成功并且通过，则插入信息到债权列表
					isInsertZqSuc = zqzrlbDao.insertZqzrlb(zqshcgMap);
					if (!isInsertZqSuc) {
						logger.info("审核债权时关联分档信息失败!");
						return false;
					}
				}else if (isSaveSHZqshcgSuc&&"03".equals(shState)) {
					logger.info("审核债权不通过,成功！ ");
					return true;
				}else {
					logger.info("更改审核状态失败,审核失败!");
					return false;
				}
			} else {
				logger.info("债权不是待审核状态,审核失败!");
				return false;
			}
		}
		}
		return true;
	}

	@Override
	public List<Map<String,String>> loadFdType() {
		return zqshcgDao.loadFdType();
	}

	@Override
	public Map<String, Object> getZqInfo(JSONObject params) {
		return zqshcgDao.getZqInfo(params);
	}

	@Override
	public int getQxDayByQxMonth(String qxMonth) {
		return zqshcgDao.getQxDayByQxMonth(qxMonth);
	}
	
}
